package com.jyb.util.capture;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.jyb.config.HttpCilentConfig;
import com.jyb.util.UrlUtil;
import com.jyb.vo.DzxdVo;

public class DzxdUtil {

	public static List<DzxdVo> capture(String tdh){
		
		List<DzxdVo> list=new ArrayList<DzxdVo>();
		
		CloseableHttpClient httpclient = HttpClients.custom().disableRedirectHandling().build();
		CloseableHttpResponse response=null;
		try {
			
			String url="http://edi.easipass.com/dataportal/query.do?ctno=&blno="+tdh+"&pagesize=30&qid=402803af0ecb1a4c010ecb1bb2b3003e";
			url=UrlUtil.hanadleUrl(url);
			HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
			response = httpclient.execute(httpGet);
			if(response.getFirstHeader("Location")==null){
				return list;
			}
			url=response.getFirstHeader("Location").getValue();//httpclient本身回自动重定向，但是空格他没有处理，会报错
			HttpClientUtils.closeQuietly(response);
			url=UrlUtil.hanadleUrl(url);
			if(url.indexOf("&vslname=&")>-1){
				//说明没有数据
				return list;
			}
			httpGet = HttpCilentConfig.getHttpGet(url);
			response = httpclient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			String str=EntityUtils.toString(entity);
			Document document=Jsoup.parse(str);
			
			Elements elements2=document.select("table[bgcolor=#EEEEEE]").get(0).nextElementSibling().select("tbody").get(0).children();
			String cm=elements2.get(1).select("td").get(1).text();
			String hc=elements2.get(2).select("td").get(1).text();
			
			Elements elements=document.select("td[class=whiteboldfont]").get(0).parent().parent().select("tr:gt(0)");
			for (Element element : elements) {
				Elements elementTds=element.select("td");
				
				DzxdVo dzxdVo=new DzxdVo();
				dzxdVo.setCm(cm);
				dzxdVo.setHc(hc);
				dzxdVo.setTdh(tdh);
				dzxdVo.setXh(elementTds.get(0).text());
				dzxdVo.setZl(elementTds.get(1).text());
				dzxdVo.setJs(elementTds.get(2).text());
				dzxdVo.setHghz(elementTds.get(3).text());
				dzxdVo.setHgjshzsj(elementTds.get(4).text());
				dzxdVo.setXjyr(elementTds.get(5).text());
				dzxdVo.setXgdm(elementTds.get(6).text());
				dzxdVo.setCostrp(elementTds.get(7).text());
				
				list.add(dzxdVo);
			}
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(response!=null){
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println(JSON.toJSONString(list));
		return list;
	}
	
	public static void main(String[] args) {
		List<DzxdVo> vos=DzxdUtil.capture("GOSUSNH8704244");
		for (DzxdVo dzxdVo : vos) {
			System.out.println(dzxdVo.getCm());
		}
	}
}
